package com.closer.server.mapper.user;

import com.mybatisflex.core.BaseMapper;
import com.closer.server.biz.user.entity.UserStatistics;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;

/**
 * 用户解题统计 映射层。
 *
 * @author closer
 */
public interface UserStatisticsMapper extends BaseMapper<UserStatistics> {

    /**
     * 根据用户id删除
     * @param id 用户id
     */
    default void deleteByUserId(Long id) {
        var queryWrapper = QueryWrapper.create().eq("user_id",id);
        deleteByQuery(queryWrapper);
    }

    /**
     * 更新用户提交数
     *
     * @param userId 用户id
     * @return 更新结果
     */
    default boolean incrementSubmitCount(Long userId) {
        return UpdateChain.of(UserStatistics.class)
               .setRaw(UserStatistics::getSubmit,"submit + 1")
               .where(UserStatistics::getUserId).eq(userId)
               .update();
    }
}
